{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "161f108a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting socket，waiting for connection...\n"
     ]
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import xml.dom.minidom\n",
    "import tkinter as tk\n",
    "from tkinter import ttk\n",
    "import socket\n",
    "import json\n",
    "\n",
    "#open xml file\n",
    "dom = xml.dom.minidom.parse('UNData.xml')\n",
    "\n",
    "#get elements in the file\n",
    "root = dom.documentElement\n",
    "\n",
    "#search by Tag\n",
    "cc = dom.getElementsByTagName('Country')\n",
    "country = []\n",
    "for c in cc:\n",
    "    # elements of country\n",
    "    \n",
    "    a = 0\n",
    "    while a < len(cc):\n",
    "        country.append(cc[a].firstChild.data)\n",
    "        a+=28\n",
    "\n",
    "yy = dom.getElementsByTagName('Year')\n",
    "year = []\n",
    "for y in yy:\n",
    "    # elements of year\n",
    "    \n",
    "    b = 0\n",
    "    while b < len(yy):\n",
    "        year.append(yy[b].firstChild.data)\n",
    "        b+=1\n",
    "\n",
    "vv = dom.getElementsByTagName('Value')\n",
    "value = []\n",
    "for v in vv:\n",
    "    # elements of value\n",
    "    \n",
    "    d = 0\n",
    "    while d < len(vv):\n",
    "        value.append(vv[d].firstChild.data)\n",
    "        d+=1\n",
    "country_info = {}\n",
    "for i in range(len(country)):\n",
    "    country_info[country[i]] = {'year':year[i*28:(i+1)*28],'value':value[i*28:(i+1)*28]}\n",
    "ip_port = (socket.gethostname(), 8080)\n",
    "sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM)            # create a socket\n",
    "sk.bind(ip_port)                # bind host address and port together\n",
    "sk.listen(5)                    # # configure how many client the server can listen simultaneously\n",
    "print('Starting socket，waiting for connection...')\n",
    "while True:\n",
    "    conn, address = sk.accept()# # accept new connection\n",
    "    # receive data stream. it won't accept data packet greater than 1024 bytes\n",
    "    while True:\n",
    "        \n",
    "        try:\n",
    "            client_data = conn.recv(1024).decode()\n",
    "        except Exception as e:\n",
    "            conn.close()\n",
    "            break\n",
    "        client_data = str(client_data)\n",
    "        dataStr = country_info[client_data]\n",
    "        dataStr = json.dumps(dataStr)\n",
    "        print(\"Message from Client %s：%s\" % (address, client_data))\n",
    "        conn.send(dataStr.encode()) # # send data to the client\n",
    "sk.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f4cca78",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
